package com.leyou.cart.controller;

import com.leyou.cart.pojo.Cart;
import com.leyou.cart.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CartController {

    @Autowired
    private CartService cartService;



    @PostMapping
    public ResponseEntity<Void> addCart(@RequestBody Cart cart){

        this.cartService.addCart(cart);


        return ResponseEntity.status(HttpStatus.CREATED).build();

    }

    @GetMapping
    public ResponseEntity<List<Cart>> queryCart(){

        List<Cart> carts = this.cartService.queryCart();

        if (carts != null && 0!=carts.size()) {
            return ResponseEntity.ok(carts);
        }
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();

    }


    @PutMapping("increment")
    public ResponseEntity<Void> updateCart(@RequestBody Cart cart){

      this.cartService.incrementCart(cart);

      return ResponseEntity.status(HttpStatus.CREATED).build();

    }

    @DeleteMapping("{skuId}")
    public ResponseEntity<Void> DeleteCart(@PathVariable("skuId")Long skuId){

        this.cartService.DeleteCart(skuId);

        return ResponseEntity.status(HttpStatus.OK).build();

    }
}
